﻿<!DOCTYPE html>
<html>
<head>
	<meta charset="UTF-8">
	<link rel="stylesheet" type="text/css" href="../themes/default/easyui.css">
	<link rel="stylesheet" type="text/css" href="../themes/icon.css">
	<link rel="stylesheet" type="text/css" href="../demo.css">
	<script type="text/javascript" src="../jquery.min.js"></script>
	<script type="text/javascript" src="../jquery.easyui.min.js"></script>
	<script type="text/javascript" src="../locale/easyui-lang-zh_CN.js"></script>
	<script type="text/javascript" src="../jquery.cookie.js"></script>
    <script type="text/javascript" src="../extend.js"></script>
    <script>
        var dataListCache = null;
        var dataCache = null;
		var dataDetailListCache = null;
        var editing = false;
        var rowEditing = -1;
		var searchArg = {};
        var appId = GetQueryString("moduleid");
        window.onresize=calSize;
        $(document).ready(function(){
            setData();
            calSize();
		});
        function pageChanging(pageNumber, pageSize) {
            showMask("查找中");
            $.get('../../api/unit/glist', { Args: searchArg, Length: pageSize, Pos: (pageNumber-1) * pageSize }, function (data) {
                if (data.Result == 0) {
                    dataListCache = JSON.parse(data.Data);
                    $("#unitgList").datagrid("loadData", dataListCache);
                    $("#unitgList").datagrid('getPager').pagination({ total: data.Count, pageNumber: pageNumber, onSelectPage: pageChanging });
                } else {
                    $.messager.alert('提示', data.Message, 'error');
                }
                hideMask();
            }, 'json');
        }
        function setData() {
            pageChanging(1,10);
        }
        window.onresize=calSize;
        function calSize(){
            var height=window.parent.document.getElementById("mainFrame").clientHeight;
            var width=window.parent.document.getElementById("mainFrame").clientWidth;
            window.parent.document.getElementById(appId).height = height-32;
            $("#unitgList").datagrid('resize',{height:height-76,width:width-15});
            $("#toolbar").panel({width:width-15});
            $("#editToolbar").panel({width:width-15});
            $("#detailEditToolBar").panel({width:width-15});
            
            $("#editZone").panel('resize',{height:height-76,width:width-15});
            $("#editZone").panel('resize',{width:width-15,height:288});
            $("#unitList").datagrid('resize',{width:width-15,height:height-402});
        }
        
        function initVal(){
            $('#AutoId').val(dataCache.AutoId);
            $('#Flag').val(dataCache.Flag);
            $('#Site').val(dataCache.Site);
			$('#State').val(dataCache.State);
            $('#Code').textbox("setValue",dataCache.Code);
            $('#Name').textbox("setValue",dataCache.Name);
            $('#Accuracy').numberbox("setValue",dataCache.Accuracy); 
			resetTable();
        }
        function createNewRecord(){
            dataCache = new Object();
            initVal();
            $('#editContent').window('open');
        }
        function editRecord(rowIndex,rowData){
            dataCache = rowData;
            initVal();
            $('#editContent').window('open');
        }
		function selectRecord(rowIndex,rowData){
			dataCache = rowData;
		}
		function deleteRecord(){
			if (dataCache == null||dataCache.AutoId==0) {
                $.messager.alert("错误", "请先选中要删除的对象", "error");
                return;
            }
			$.messager.confirm('删除提示', '确定删除该记录?', function(r){
				if (r){
					showMask();
					$.get("../../api/unit/removewithdetail?id="+dataCache.AutoId,function(response){
						if(response.Result==0){
							for(var i=0;i<dataListCache.length;i++){
								if(dataListCache[i].AutoId == dataCache.AutoId){
									dataListCache.splice(i,1);
									break;
								}
							}	
							dataCache = null;	
							$('#unitList').datagrid('loadData', dataListCache); 
							$.messager.alert("信息","操作成功","info");									
						}else{
							$.messager.alert("错误",response.Message,"error");
						}
					})
					.error(function(){
						$.messager.alert("错误","系统内部错误","error");
					})
					.complete(function() { 
						hideMask();
					});	
				}
			});		
		}
        function submitChange(){
            if (dataCache == null) {
                $.messager.alert("错误", "无法获取被编辑的对象", "error");
                return;
            }
            showMask();
			var oldData = JSON.parse(JSON.stringify(dataCache));
			dataCache.State = $("#State").val();
            dataCache.Flag = $("#Flag").val();
            dataCache.Code = $("#Code").textbox("getValue");
            dataCache.Name = $("#Name").textbox("getValue");
            dataCache.Accuracy = $("#Accuracy").numberbox("getValue");
            $.post("../../api/unit/gset",dataCache,function(response){
                if(response.Result==0){
                    dataCache = JSON.parse(response.Data);
					submitDetail();
					if(dataCache.Flag != 1){
                        for(var i=0;i<dataListCache.length;i++){
                            if(dataListCache[i].AutoId == dataCache.AutoId){
                                dataListCache.splice(i,1,dataCache);
								console.log('列表第'+i+'项被更新');
                                break;
                            }
                        }
                    }else{
						dataListCache.splice(0,0,dataCache);
						console.log('新项被追加到列表');
					}
                    $('#unitgList').datagrid('loadData', dataListCache); 
                    $.messager.alert("信息","操作成功","info");
                    $('#editContent').window('close');
                }else{
                    $.messager.alert("错误",response.Message,"error");
					dataCache = oldData;
                }
            })
            .error(function(){
                $.messager.alert("错误","系统内部错误","error");
				dataCache = oldData;
            })
            .complete(function() { 
                hideMask();
            });
        }
        function boolFormatter(value,row,index){
            if(value){
                return "是";
            }else{
                return "否";
            }
        }
        function listUnitForG(Gid){
			$.get('../../api/unit/listbyg?gid='+Gid,function(data){if(data.Result==0){$('#unitList').datagrid('loadData',JSON.parse(data.Data));}});
        }
        function addItem(){
            if(editing){
				$('#unitList').datagrid('endEdit',rowEditing);
                $('#unitList').datagrid('insertRow',{index:rowEditing+1,row:{AutoId:0,Flag:0,UnitGroupId:dataCache.AutoId}});
            }else{
                $.messager.alert("消息","先按编辑键进入编辑状态","info");
            }
        }
		function removeItem(){
			if(rowEditing!=-1){
				$('#unitList').datagrid('cancelEdit', rowEditing).datagrid('deleteRow',rowEditing);
			}else{
				$.messager.alert('提示','需要整行选中才能删除');
			}
        }
        function setRowIndex(index,row){
            rowEditing = index;
        }
        function editTable(){
            if(!editing){
                $('#unitList').datagrid("enableCellEditing"); 
                editing=true;
                $('#detailAdd').linkbutton("enable"); 
                $('#detailDel').linkbutton("enable"); 
                $('#detailSave').linkbutton("enable"); 
                $('#detailCancel').linkbutton("enable"); 
                $('#detailEdit').linkbutton("disable"); 

            }else{
                $.messager.alert("消息","当前正在编辑中","info");
            }
        }
        function endEditTable(){
            $('#unitList').datagrid('endEdit',rowEditing);
            $('#unitList').datagrid("disableCellEditing"); 
            rowEditing = -1;
            editing = false;
            $('#detailAdd').linkbutton("disable"); 
            $('#detailDel').linkbutton("disable"); 
            $('#detailSave').linkbutton("disable"); 
            $('#detailCancel').linkbutton("disable"); 
            $('#detailEdit').linkbutton("enable"); 
        }
        function resetTable(){
			if (dataCache.AutoId) {
                listUnitForG(dataCache.AutoId);
            }else{
				$('#unitList').datagrid('loadData',[]);
				$('#unitList').datagrid('loadData',[]);
				$('#unitList').datagrid('loadData',[]);
				$('#unitList').datagrid('loadData',[]);
				$('#unitList').datagrid('loadData',[]);
				$('#unitList').datagrid('loadData',[]);
			}
            endEditTable();
        }
        function submitDetail(){
			var asnycState = $.ajaxSettings.async ;
			$.ajaxSettings.async = false;
            if(rowEditing!=-1){
                var valid = $('#unitList').datagrid('validateRow',rowEditing);
                if(!valid){
                    $.messager.alert("错误","错误的数据格式","error");
                    return;
                }
                $('#unitList').datagrid('endEdit',rowEditing);
			}
			endEditTable();
			var data = $('#unitList').datagrid('getAllRows');
			$.post("../../api/unit/setunits",{MasterId:dataCache.AutoId,Data:data},function(response){
				if(response.Result==0){
					dataCache.Units = JSON.parse(response.Data);
					for(var i=0;i<dataCache.Units.length;i++){
						dataDetailListCache.push(dataCache.Units[i]);
					}
				}else{
					$.messager.alert("错误",response.Message,"error");
				}
			}).error(function(){$.messager.alert("错误","系统内部错误","error");}).complete(function() { });
			$.ajaxSettings.async = asnycState;
        }
        function loadDataCache(rowIndex,rowData){
            dataCache = rowData;
        }
    </script>
<head>
<body onload = "hideMask()">
    <script>showMask('正在载入数据');</script>
	<div id="toolbar" class="easyui-panel" style="padding:1px;">
        <a href="#" class="easyui-linkbutton" data-options="onClick:createNewRecord,iconCls:'icon-edit',plain:true">新建</a>
        <a href="#" class="easyui-linkbutton" data-options="onClick:deleteRecord,iconCls:'icon-remove',plain:true">删除</a>
        <a href="#" class="easyui-linkbutton" data-options="onClick:setData,iconCls:'icon-search',plain:true">搜索</a>
	</div>
    <div style="margin:2px"></div>
    <table id="unitgList" class="easyui-datagrid"
        data-options="pagination:true,rownumbers: true,singleSelect:true,method:'get',onClickRow:loadDataCache,onDblClickRow:editRecord,striped:true,">
        <thead>
            <tr>
                <th data-options="field:'AutoId',hidden:true"></th>
                <th data-options="field:'Flag',hidden:true"></th>
				<th data-options="field:'State',hidden:true"></th>
                <th data-options="field:'Code',width:120,align:'right'">单位组代号</th>
                <th data-options="field:'Name',width:120,align:'right'">单位组名称</th>
                <th data-options="field:'Accuracy',width:120,align:'right'">单位组精度</th>
            </tr>
        </thead>
    </table>
    <div id="editContent" class="easyui-window" title="编辑框" data-options="noheader:true,maximized:true,modal:true,closed:true,iconCls:'icon-save',border:false" style="padding:5px 5px 0px 5px">
        <div class="easyui-panel" id="editToolbar" style="padding:1px;width:100%">
            <a href="#" class="easyui-linkbutton" data-options="onClick:submitChange,iconCls:'icon-save',plain:true">保存</a>
            <a href="#" class="easyui-linkbutton" data-options="onClick:initVal,iconCls:'icon-no',plain:true">取消</a>
            <a href="#" class="easyui-linkbutton" data-options="onClick:function(){$('#editContent').window('close');},iconCls:'icon-blank',plain:true">关闭</a>
        </div>
        <div style="margin:2px"></div>
        <div id="editZone" class="easyui-panel" style="padding:4px 4px 0px 4px" >
            <table>
                <tr>
                    <td><input class="easyui-textbox" id="Code"  style="width:300px" data-options="label:'单位组代号:',required:true"></td>
                    <td>&nbsp;</td>
                    <td><input class="easyui-textbox" id="Name"  style="width:300px" data-options="label:'单位组名称:'"></td>
                </tr>
                <tr colspan=3>
                    <td><input class="easyui-numberbox"  id="Accuracy" data-options="label:'单位精度',precision:0"/></td>
                </tr>
            </table>
            <div hidden>
                <p id="AutoId"></p>
                <p id="Flag"></p>
                <p id="Site"></p>
				<p id="State"></p>
            </div>
        </div>
		<div style="margin:2px"></div>
        <div class="easyui-panel" id="detailEditToolBar" style="padding:1px;width:100%">
            <a href="#" id="detailEdit" class="easyui-linkbutton" data-options="onClick:editTable,iconCls:'icon-edit',plain:true">开始</a>
            <a href="#" id="detailAdd"  class="easyui-linkbutton" data-options="onClick:addItem,iconCls:'icon-add',plain:true,disabled:true">增行</a>
            <a href="#" id="detailDel"  class="easyui-linkbutton" data-options="onClick:removeItem, iconCls:'icon-remove',plain:true,disabled:true">删行</a>
            <a href="#" id="detailCancel"   class="easyui-linkbutton" data-options="onClick:resetTable,iconCls:'icon-no',plain:true,disabled:true">取消</a>
		</div>
        <div style="margin:2px"></div>
        <table id="unitList" class="easyui-datagrid" style="width:100%;"  data-options="singleSelect:true,height:100,onClickRow:setRowIndex">
            <thead>
                <tr>
                    <th data-options="field:'AutoId',hidden:true"></th>
                    <th data-options="field:'Flag',hidden:true"></th>
					<th data-options="field:'State',hidden:true"></th>
                    <th data-options="field:'UnitGroupId',hidden:true"></th>
                    <th data-options="field:'Code',width:180,align:'right',editor:{type:'textbox'}">单位代号</th>
                    <th data-options="field:'Name',width:180,align:'right',editor:{type:'textbox'}">单位名称</th>
                    <th data-options="field:'Rate',width:100,align:'right',editor:{type:'numberbox',precision:2}">换算率系数</th>
                    <th data-options="field:'RateBase',width:100,align:'right',editor:{type:'numberbox',precision:2}">换算率基数</th>
                </tr>
            </thead>
        </table>
	</div>
</body>
</html>